﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using psafth.RCTiming.Main.Controls;

namespace psafth.RCTiming.Main.Pages
{
    /// <summary>
    /// Interaction logic for EventPage.xaml
    /// </summary>
    public partial class EventPage : Page
    {
        private MainWindow _mainWindow;

        public DBContext DbContext
        {
            get
            {
                return this._mainWindow.DbContext;
            }
            set
            {
                this._mainWindow.DbContext = value;
            }
        }

        public EventPage(MainWindow m)
        {
            _mainWindow = m;
            InitializeComponent();

            this.Loaded += EventPage_Loaded;
        }

        void EventPage_Loaded(object sender, RoutedEventArgs e)
        {
            DateTime today = DateTime.Parse(DateTime.Now.ToShortDateString());

            foreach (Event ev in DbContext.Events.Where(ev => ev.EndDate >= today).OrderBy(ev => ev.EndDate))
            {
                spCurrentEvents.Children.Add(new EventDisplayControl(ev, this._mainWindow));

                foreach (IGrouping<int, EventParticipant> raceClass in ev.Participants.GroupBy(r => r.RaceClassId))
                {
                    RaceClass rc = DbContext.RaceClasses.FirstOrDefault(r => r.Id == raceClass.Key);

                    //foreach (EventParticipant t in raceClass)
                    //{
                    //    Driver driver = DbContext.Drivers.FirstOrDefault(d => d.Id == t.DriverId);
                    //}
                }
            }

            foreach (Event ev in DbContext.Events.Where(ev => ev.EndDate < today))
            {
                spPassedEvents.Children.Add(new EventDisplayControl(ev, this._mainWindow));

                foreach (IGrouping<int, EventParticipant> raceClass in ev.Participants.GroupBy(r => r.RaceClassId))
                {
                    RaceClass rc = DbContext.RaceClasses.FirstOrDefault(r => r.Id == raceClass.Key);

                    foreach (EventParticipant t in raceClass)
                    {
                        Driver driver = DbContext.Drivers.FirstOrDefault(d => d.Id == t.DriverId);
                    }
                }
            }
        }

        private void btnCreateEvent_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.CreateEventDialog prd = new Dialogs.CreateEventDialog(_mainWindow);
            prd.Owner = ParentWindow;
            prd.ShowDialog();
        }

        private Window ParentWindow
        {
            get
            {
                return Window.GetWindow(this);
            }
        }
    }
}
